package Two;

import Two.ExaminationSystem.Question.Problem;
import Two.ExaminationSystem.RandomAcquirement;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

class ServerThread extends Thread {

    //用于与考生端进行收发交流
    Socket socket;
    DataOutputStream out = null;
    DataInputStream in = null;

    RandomAcquirement acquirement = new RandomAcquirement();//抽题系统
    Problem[] problems = new Problem[3];//题库
    String[] type = new String[]{"单选题", "多选题", "判断题"};//题型
    String[] C;//存储选项内容
    String question;
    int t;//题型
    int n;//题库中的对应题序号
    int number = 0;//已出题目数量

    public ServerThread(Socket socketAtServer) {
        this.socket = socketAtServer;
        try {
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {//准备题目
            problems[0] = new Problem("E:\\方秋慧\\JAVA程序设计\\单选.txt", 4);
            problems[1] = new Problem("E:\\方秋慧\\JAVA程序设计\\多选.txt", 4);
            problems[2] = new Problem("E:\\方秋慧\\JAVA程序设计\\判断.txt", 2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (number<15) {//出够15道题就停止
            //出题
            number++;
            System.out.println("正在抽取题目……");
            t = acquirement.setType();//抽取题型
            n = acquirement.setNumber();//抽取对应题号
            question = "[" + type[t] + "] " + (number) + "、" + problems[t].getQuestion(n).getQ();
            C = problems[t].getQuestion(n).getChoice();//设置选项内容
            System.out.println("抽取题目成功！正在载入选项……");
            String choice = null;
            if (t != 2) {
                choice = C[0] + "\n" + C[1] + "\n" + C[2] + "\n" + C[3] + "\n";
            }
            String b = question + "\n" + t + "\n" + choice;

            System.out.println("正在传送选项……");
            try {
                out.writeUTF(b);//将题目给客户端
                System.out.println("选项已经送达考生端！");
                //获取客户端的答案
                System.out.println("正在等待考生答题……");
                String answer = in.readUTF();
                System.out.println("获取考生回答，正在进行判题……");
                String cr_answer = problems[t].getQuestion(n).getAnswer();//正确答案
                System.out.println("判题成功！正在传送结果……");
                //返回正确答案和正误情况
                if (answer.equals(cr_answer)) {//判断正误
                    out.writeUTF(cr_answer + "\n" + "1");//返回正确答案和结果，1为答对
                } else {
                    out.writeUTF(cr_answer + "\n" + "0");//0为答错
                }
                System.out.println();
            } catch (IOException e) {//断开连接
                System.out.println("考生离开");
                break;
            }
        }
    }
}

public class ExaminationServer {
    public static void main(String args[]) {
        ServerSocket server = null;
        Socket socketAtServer = null;
        while (true)//服务端持续接收考生的考试请求
        {
            try {
                server = new ServerSocket(9870);
            } catch (IOException e) {
                System.out.println("正在等待考生接入");
            }
            try {
                socketAtServer = server.accept();
                System.out.println("来自:" + socketAtServer.getInetAddress() + "的考生");
            } catch (IOException e) {
                System.out.println("正在等待考生");
            }
            if (socketAtServer != null) {
                ServerThread serverThread = new ServerThread(socketAtServer);
                serverThread.start(); //为每个客户端启动一个专门的线程
            } else {
                continue;
            }
        }
    }
}